package com.xdtech.hotel.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.ProtocolCmdDao;
import com.xdtech.hotel.model.ProtocolCmd;
import com.xdtech.hotel.protocol.manager.OperationManager;
import com.xdtech.hotel.protocol.vo.RCUDevice;
import com.xdtech.hotel.service.ProtocolCmdService;
import com.xdtech.hotel.vo.ProtocolCmdItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-05-25 21:41:22
 * @since 1.0
 * @see
 */
@Service
public class ProtocolCmdServiceImpl implements ProtocolCmdService {
	private Log log = LogFactory.getLog(ProtocolCmdServiceImpl.class);
	@Autowired
	private ProtocolCmdDao protocolCmdDao;
	@Autowired
	private BaseDao baseDao;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param entity
	 */
	public void save(ProtocolCmd entity) {
		protocolCmdDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<ProtocolCmd> entities) {
		protocolCmdDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param entity
	 */
	public void delete(ProtocolCmd entity) {
		protocolCmdDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		protocolCmdDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param id
	 * @return
	 */
	public ProtocolCmd get(Long id) {
		return protocolCmdDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @return
	 */
	public List<ProtocolCmd> getAll() {
		return protocolCmdDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> protocolCmds = null;
		long rows = 0;
		if (pg!=null) {
			Page<ProtocolCmd> page = protocolCmdDao.findPage(pg,"from ProtocolCmd order by createTime desc", values);
			protocolCmds = BeanUtils.copyListProperties(
					ProtocolCmdItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<ProtocolCmd> protocolCmdList = protocolCmdDao.find("from ProtocolCmd order by id desc", values);
			protocolCmds = BeanUtils.copyListProperties(
					ProtocolCmdItem.class, protocolCmdList);
			rows = protocolCmds.size();
		}
		results.put("total", rows);
		results.put("rows", protocolCmds);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateProtocolCmd(ProtocolCmdItem item) {
		ProtocolCmd protocolCmd = null;
		if (item.getId()==null) {
			protocolCmd = new ProtocolCmd();
		}else {
			protocolCmd = protocolCmdDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(protocolCmd, item);
		protocolCmdDao.save(protocolCmd);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param protocolCmdId
	 * @return
	 */
	public ProtocolCmdItem loadProtocolCmdItem(Long protocolCmdId) {
		ProtocolCmd protocolCmd = protocolCmdDao.get(protocolCmdId);
		ProtocolCmdItem protocolCmdItem = new ProtocolCmdItem();
		BeanUtils.copyProperties(protocolCmdItem, protocolCmd);
		return protocolCmdItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteProtocolCmdInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteProtocolCmdInfo(List<Long> protocolCmdIds) {
		for (Long id : protocolCmdIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-25 21:41:22
	 * @modified by
	 * @return
	 */
	@Override
	public List<ProtocolCmdItem> loadItems() {
		List<ProtocolCmd> protocolCmds = getAll();
		return ProtocolCmdItem.createItems(protocolCmds);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-5-25下午11:15:57
	 * @modified by
	 * @param device
	 * @param cmdId
	 */
	@Override
	public void secondResultHandler(RCUDevice device,String rs,Long cmdId) {
		ProtocolCmd protocolCmd = get(cmdId);
	    protocolCmd.setSecondResult(rs);
	    if (rs!=null&&rs.contains("true")) {
	    	protocolCmd.setResult("success");
		}else {
			protocolCmd.setResult("fail");
		}
	    save(protocolCmd);
	}

}
